package com.intranet.sys.dao;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.intranet.sys.domain.User;
import com.water.dao.BaseDao;
import com.water.dao.Page;

@Repository
public class UserDao extends BaseDao<User> {

	private final String GET_USER_BY_USERNAME = "from User u where u.username = ?";

	/**
	 * 查询所有用户
	 * 
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	public Page queryUserList(int pageNo, int pageSize,String param) {
		StringBuffer sb = new StringBuffer();
		sb.append("from User where 1=1");
		if(param!=null && !param.equals("")){
			sb.append(" and (username like '%"+param+"%' or realname like '%"+param+"%' or email like '%"+param+"%' or phone like '%"+param+"%' or state = '"+param+"')");
		}
		sb.append(" order by id");
		return pagedQuery(sb.toString(), pageNo, pageSize);
	}

	/**
	 * 根据用户名查询用户
	 * @param username
	 * @return
	 */
	public User getUserByUserName(String username) {
		List<User> users = (List<User>) getHibernateTemplate().find(GET_USER_BY_USERNAME,username);
		if (users.size() == 0) {
			return null;
		} else {
			return users.get(0);
		}
	}
}
